package com.zhdl.modules.iiiiiii.jSerialCommTest;

import com.fazecast.jSerialComm.SerialPort;
import com.fazecast.jSerialComm.SerialPortDataListener;
import com.fazecast.jSerialComm.SerialPortEvent;
import com.zhdl.common.ytools.callback.CommonCallback;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
@Slf4j
@Service
public class JSerialCommService {
    private SerialPort serialPort;
    private int sendId = 0;
    private LinkedList<CommonCallback<String>> sendQueen = new LinkedList<>();
    private int receiverId = 0;
    public boolean openSerial(String port){
        serialPort = SerialPort.getCommPort(port); // 替换为你的端口名
        serialPort.setComPortParameters(9600, 8, SerialPort.ONE_STOP_BIT, SerialPort.NO_PARITY); // 设置端口参数
        serialPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_BLOCKING, 1, 0);
        if (serialPort.openPort()) {
            System.out.println("Port opened successfully.");
            addDateListener();
            return true;
        } else {
            System.out.println("Unable to open the port.");
            return false;
        }
    }

    private void addDateListener() {
        // 创建数据监听器
        serialPort.addDataListener(new SerialPortDataListener() {
            @Override
            public int getListeningEvents() {
                return SerialPort.LISTENING_EVENT_DATA_AVAILABLE;
            }

            @Override
            public void serialEvent(SerialPortEvent event) {
                if (event.getEventType() == SerialPort.LISTENING_EVENT_DATA_AVAILABLE) {
                    // 接收数据
                    byte[] readBuffer = new byte[1024]; // 调整数组大小以适应预期的响应长度
                    int numRead = serialPort.readBytes(readBuffer, readBuffer.length);
                    if (numRead > 0) {
                        // 将读取的字节转换为十六进制字符串
                        StringBuilder data = new StringBuilder();
                        for (int i = 0; i < numRead; i++) {
                            data.append(String.format("%02X ", readBuffer[i]));
                        }
                        if (sendQueen.size()>receiverId){
                            sendQueen.get(receiverId).statusCallback(data.toString() +"----"+ receiverId);
                            receiverId++;
                        }else{
                            log.info("收到无发送返回指令"+data);
                        }
                        if (receiverId>=99999){
                            receiverId = 0;
                        }
                       // System.out.println("Received data: " + data.toString());
                        // 在这里进行收到数据的处理操作，例如加入阻塞队列，另一个模块消费队列解析收到的数据
                    }
                }
            }
        });
    }

    public synchronized boolean sendDate(byte[] date,CommonCallback<String> callback){
        if (serialPort == null){
            return false;
        }
        sendId++;
        if (sendId>=99999){
            sendId =0;
            sendQueen.clear();
        }
        sendQueen.add(callback);
        serialPort.writeBytes(date, date.length);
        return true;
    }

}
